<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------

namespace app\model;

use \think\Model;

class DistributionSetting extends Model
{
    protected $name               = 'distribution_setting';
    protected $autoWriteTimestamp = false;
    protected $insert             = [];
    protected $update             = [];

    /**
     * 获取等级信息
     * @param int $level 等级
     * @param int $return_radio 是否返回佣金比例
     * @return array
     */
    public static function getLevelInfo(int $level, int $return_radio = 0)
    {
        $info  = self::field('id, type, value')->where('uuid', 'setting')->find();
        $value = json_decode($info['value'], true);
//        $level = $level == 2 ? $value[1] : $value[0];
        $level = $value[$level - 1];
        if (!$return_radio) {
            $res = [
                'name' => $level['name'] ?? '',
                'icon' => !empty($level['icon']) ? get_file_path($level['icon'], 1) : '',
            ];
        } else {
            $res = [
                'name'        => $level['name'] ?? '',
                'type'        => $info['type'],
                'icon'        => !empty($level['icon']) ? get_file_path($level['icon'], 1) : '',
                'ratio'       => !empty($level['ratio']) ? intval($level['ratio']) : 0,
                'ratio_one'   => !empty($level['ratio_one']) ? intval($level['ratio_one']) : 0,
                'order_count' => $level['order_count'] ?? 0,
                'team_ratio'  => $level['team_ratio'] ?? 0,
            ];
        }
        return $res;
    }
}
